package com.yida.wy.common.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yida.common.core.utils.StringUtils;
import com.yida.wy.common.constant.WyConstants;
import com.yida.wy.common.domain.WyBaseBusinessNum;
import com.yida.wy.common.mapper.WyBaseBusinessNumMapper;
import com.yida.wy.common.service.IWyBaseBusinessNumService;

import cn.hutool.core.date.DateUtil;

/**
 * 业务编号Service业务层处理
 *
 * @author lqq
 * @date 2022-11-14
 */
@Service
public class WyBaseBusinessNumServiceImpl implements IWyBaseBusinessNumService {

    @Autowired
    private WyBaseBusinessNumMapper businessNumMapper;

    /**
     * 获取下一个业务编号
     *
     * @param areaId 地区ID
     * @param bizFlagId 业务标识ID
     * @return 业务编号
     */
    @Override
    public WyBaseBusinessNum getNextNum(Long areaId, Integer bizFlagId) {
        int thisYear = DateUtil.thisYear();
        WyBaseBusinessNum temp = new WyBaseBusinessNum();
        temp.setYear(thisYear);
        temp.setAreaId(areaId);
        temp.setBizFlagId(bizFlagId);
        WyBaseBusinessNum businessNum = businessNumMapper.selectGwjBaseBusinessNum(temp);
        // 当年还未做业务
        if (StringUtils.isNull(businessNum)) {
            businessNum = new WyBaseBusinessNum();
            businessNum.setYear(thisYear);
            businessNum.setAreaId(areaId);
            businessNum.setBizFlagId(bizFlagId);
            businessNum.setSeq(WyConstants.BUSINESS_NUM_SEQ_FIRST);
            businessNumMapper.insertGwjBaseBusinessNum(businessNum);
        }
        // 是当年并且已经开始业务，编号序列+1后便是新的业务编号
        else {
            businessNum.setSeq(businessNum.getSeq() + 1);
            businessNumMapper.updateGwjBaseBusinessNum(businessNum);
        }
        return businessNum;
    }
}
